Skip to content

Improve builder value fill#20

Merged
filiphr merged 8 commits into
mapstruct:mainfrom
hduelme:improve-builder-value-fill
Mar 15, 2026
Merged

Improve builder value fill#20
filiphr merged 8 commits into
mapstruct:mainfrom
hduelme:improve-builder-value-fill

Conversation

@hduelme

@hduelme hduelme commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

I changed the generated code to use java switch string matching, if there are more than 3 strings to check. For this I extracted the builder fill code into fillBuilder macro.
I although changed the loop iteration to use entrySet.

@hduelme

hduelme commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator Author

I tested the this improvement with mapstruct and found a small improvement (about 100ms for mapstruct-processor in MapperGem), manly due to the reduction in String comparison.

@filiphr filiphr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads way better @hduelme. Thanks a lot for the improvements. I've left some comments.

Apart of the comments, I see that we have a lot of values.get( methodName ) and defaultMethod.getValue(). Won't it be a bit better if we assign them at the beginning of the if / switch?

e.g.

String methodName = defaultMethod.getKey();
switch ( methodName ) {
    case "myClassWithDefault":
        builder.setMyclasswithdefault( GemValue.create( values.get( methodName ), defaultMethod.getValue(), TypeMirror.class ) );
        break;

Would become

String methodName = defaultMethod.getKey();
AnnotationValue defaultValue = defaultMethod.getValue();
AnnotationValue value = values.get( methodName );
switch ( methodName ) {
    case "myClassWithDefault":
        builder.setMyclasswithdefault( GemValue.create( value, defaultValue, TypeMirror.class ) );
        break;

Comment thread processor/src/main/resources/org/mapstruct/tools/gem/processor/Gem.ftl Outdated
Comment thread processor/src/main/resources/org/mapstruct/tools/gem/processor/Gem.ftl Outdated
Comment thread processor/src/main/resources/org/mapstruct/tools/gem/processor/Gem.ftl Outdated
@filiphr

filiphr commented Feb 14, 2026

Copy link
Copy Markdown
Member

I tested the this improvement with mapstruct and found a small improvement (about 100ms for mapstruct-processor in MapperGem), manly due to the reduction in String comparison.

That's great

@hduelme

hduelme commented Feb 15, 2026

Copy link
Copy Markdown
Collaborator Author

@filiphr I've addressed your comments. I agree that moving defaultValue and value in front of the switch/if statement improves readability. I've made that change.

Comment thread processor/src/main/resources/org/mapstruct/tools/gem/processor/Gem.ftl Outdated
Comment thread processor/src/main/resources/org/mapstruct/tools/gem/processor/Gem.ftl Outdated
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
@filiphr filiphr merged commit 949a211 into mapstruct:main Mar 15, 2026
6 checks passed
@filiphr

filiphr commented Mar 15, 2026

Copy link
Copy Markdown
Member

Thanks a lot @hduelme. Sorry, that it took me some time to merge this. I'll try to setup the release pipeline for this so that I can do a new release in order for us to use this

@filiphr filiphr added the enhancement New feature or request label Mar 15, 2026
@filiphr filiphr added this to the 1.0.0.Alpha4 milestone Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants